package com.barrett.hvzmoderator.com.barrett;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.alexd.jsonrpc.JSONRPCClient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.barrett.hvzmoderator.R;

public class hvzplayerinfo extends Activity {
	String sessid;
	Bitmap avatar;
	ProgressDialog dialog;
	ImageView avatarView;
	private hvzplayer player;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playerinfo);
        
        player = new hvzplayer(getIntent().getStringExtra("player"));
        player.avatar = getIntent().getStringExtra("avatar");
        sessid = getIntent().getStringExtra("sessid");
        
        
        TextView name = (TextView) findViewById(R.id.info_name);
        TextView killId = (TextView) findViewById(R.id.info_killid);
        TextView status = (TextView) findViewById(R.id.info_status);
        TextView tags = (TextView) findViewById(R.id.info_tags);
        avatarView = (ImageView) findViewById(R.id.info_avatar);
        
        dialog = new ProgressDialog(this);
		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		dialog.setMessage("Loading...");
		dialog.setCancelable(false);
		dialog.show();
		
        Thread img = new Thread () {
        	public void run () {
        		try {
        			URL aURL = new URL(player.avatar);
        			URLConnection conn = aURL.openConnection();
        			conn.connect();
        			InputStream is = conn.getInputStream();
        			/* Buffered is always good for a performance plus. */
        			BufferedInputStream bis = new BufferedInputStream(is);
        			/* Decode url-data to a bitmap. */
        			avatar = BitmapFactory.decodeStream(bis);
        			bis.close();
        			is.close();
        			/* Apply the Bitmap to the ImageView that will be returned. */
        		} catch (IOException e) {
        			Log.e("DEBUGTAG", "Remtoe Image Exception", e);
        		}
        		Message msgEnd = new Message();
				Bundle bEnd = new Bundle();
				bEnd.putInt("prog", dialog.getMax());
				msgEnd.setData(bEnd);
				imgHandler.sendMessage(msgEnd);
        	}
        };
        img.start();

        name.setText(player.name);
        name.setTextSize(36);
        killId.setText("KillID: " + player.killid);
        status.setText("KillID: " + player.statusToString());
        tags.setText("Tags: " + Integer.toString(player.tags));
        
        Toast.makeText(getApplicationContext(), "Press Menu for actions", Toast.LENGTH_SHORT).show();
    }
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
    	menu.clear();
    	MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_player, menu);
        return true;	
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
    	case R.id.playerBack:
    		Intent intentBack = new Intent();
            setResult(RESULT_OK, intentBack);
            finish();
            break;
    	case R.id.playerEdit:
    		Intent intentEdit = new Intent(this.getBaseContext(), hvzedit.class);
    		intentEdit.putExtra("uid", player.uid);
    		intentEdit.putExtra("name", player.name);
    		intentEdit.putExtra("killid", player.killid);
    		intentEdit.putExtra("status", player.status);
    		intentEdit.putExtra("tags", player.tags);
    		startActivityForResult(intentEdit, 1);
    		
    		
    		break;
    	case R.id.playerAch:
    		Intent intentAch = new Intent(this.getBaseContext(), hvzachievements.class);
    		    		
    		intentAch.putExtra("sessid", sessid);
    		intentAch.putExtra("uid", player.uid);
    		startActivityForResult(intentAch, 0);
    	}
    	return true;
    }
    
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    	TextView name = (TextView) findViewById(R.id.info_name);
        TextView killId = (TextView) findViewById(R.id.info_killid);
        TextView status = (TextView) findViewById(R.id.info_status);
        TextView tags = (TextView) findViewById(R.id.info_tags);
    	switch(resultCode) {
    	case 0:
    		//update achs if on playerinfo
    		
    		break;
    		
    	case 1:
    		player = new hvzplayer(intent.getStringExtra("player"));
    		JSONRPCClient client = JSONRPCClient.create("http://www.ohiostateugc.com/services/json-rpc");
        	try {
        		String obt =  client.callString("hvz.players.edit", sessid, player.toString());
	    		name.setText(player.name);
	            name.setTextSize(36);
	            killId.setText("KillID: " + player.killid);
	            status.setText("KillID: " + player.statusToString());
	            tags.setText("Tags: " + Integer.toString(player.tags));
	            Toast.makeText(getApplicationContext(), "Player Saved", Toast.LENGTH_SHORT).show();
        	}
        	catch (Exception e) {
        		Toast.makeText(getApplicationContext(), "Error " + e.toString(), Toast.LENGTH_SHORT).show();
        	}
    		
    		break;
    	}
    	
    }
    
    private Handler imgHandler = new Handler(){
		  
		  @Override
	      public void handleMessage(Message msg) {  
			  avatarView.setImageBitmap(avatar);	  
			  
			  dialog.dismiss();
		  }
	  };
    
}
